<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        const arr1 = [1, 2, 3, 4, 5];
        const arr2 = [6, 7, 8, 9];

        // 迭代器创建函数  iterator creator
        function createIterator(arr) {
            return {
                i: 0,
                next() {
                    var result = {
                        value: arr[this.i],
                        done: this.i >= arr.length
                    }
                    this.i++
                    // console.log(result);

                    return result
                }
            }
        }

        const iter1 = createIterator(arr1);
        const iter2 = createIterator(arr2);


        function logIterator(arr) {
            let data = arr.next()
            while (!data.done) {
                console.log(data);
                //进行下一次迭代循环
                data = arr.next()
            }
        }

        logIterator(iter1)
        logIterator(iter2)




    </script>
</body>

</html>